import { Button, message, Modal, Divider} from 'antd';
import React, {  useRef } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';
import { queryDictList, deleteDict } from '@/services/dict';
import { connect, router as Router } from 'dva';
import { CurrentUser } from '@/models/user'

// import {router as Router} from 'dva'
const { confirm } = Modal;


const TableList: React.FC<{ currentUser: CurrentUser }> = ({ currentUser }) => {
  const actionRef = useRef<ActionType>();
  const columns: ProColumns<any>[] = [
    {
      title: '模糊搜索',
      dataIndex: 'desc',
      hideInForm: true,
      hideInTable: true,
      initialValue: ''
    },
    {
      title: '描述',
      dataIndex: 'desc',
      hideInSearch: true,
      render: (text, record:any) => (
        <Router.Link to={`/setting/dict/edit?ID=${record.ID}`}>{text}</Router.Link>
      )
    },
    {
      title: '类型',
      dataIndex: 'type',
      hideInSearch: true
    },
    {
      title: '标签',
      dataIndex: 'label',
      hideInSearch: true
    },
    {
      title: '键值',
      dataIndex: 'value',
      hideInSearch: true
    },
    {
      title: '排序',
      dataIndex: 'sort',
      hideInSearch: true
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record:any) => (
        <>
          <Button size="small" type="primary" onClick={() => {
            confirm({
              title: '温馨提示',
              content: '确定删除记录？',
              onOk() {
                deleteDict({ ID: record.ID }).then(res => {
                  if (res.code == 0) {
                    message.info('删除成功！')
                    if (actionRef && actionRef.current) {
                      actionRef.current.reload();
                    }
                  }
                })
              }
            });
          }}>
            删除
        </Button>
        <Divider type="vertical" />
        <Router.Link to={`/setting/dict/edit?desc=${record.desc}&type=${record.type}&`}>添加键值</Router.Link>
        </>
      ),
    },

  ];

  return (
    <PageHeaderWrapper>
      <ProTable<any>
        headerTitle=""
        actionRef={actionRef}
        rowKey="ID"
        toolBarRender={(action, { selectedRows }) => [
          <Router.Link to={`/setting/dict/edit`}>添加</Router.Link>
        ]}
        // params={params}
        request={(params: any) => queryDictList(params)}
        pagination = {{defaultPageSize:10}}
        columns={columns}
        rowSelection={{}}
        tableAlertRender={() => (false)}
      // search={{}}
      />
    </PageHeaderWrapper>
  );
};

export default connect(({ loading }: { loading: { effects: { [key: string]: boolean } } }) => ({
  submitting: loading.effects['machine/saveMachine'],
}))(TableList);